//@author A0096466N
//
// **********
//
// autoeditstatus class takes in the entire storage of tasks via vector<Task>
// and checks for 2 things.
//
// first it identifies which tasks are DONE and are at least 1 week in the past.
// if so, it changes DONE -> ARCHIVE
//
// second, it identifies which tasks are WIP and are overdue.
// if so, it changes WIP -> CMI
//
// **********

#include "AutoEditStatus.h"

AutoEditStatus::AutoEditStatus(void) {

}

AutoEditStatus::~AutoEditStatus(void) {

}

bool AutoEditStatus::archive(vector<Task> & store,const time_t & today) {
	
	assert(store.size() != NIL);
	assert(today != NIL);

	ofstream writeFile;
	writeFile.open(FILENAME_LOGGER.c_str(), ofstream::out | ofstream::app);
	writeFile << "Attempting to archive" << endl;
	writeFile.close();

	bool edited = false;

	for(vector<Task>::iterator iter = store.begin(); iter != store.end(); iter++) {
		
		// if done && 7 days in the past, mark as archive
		if((*iter).getDateInSeconds() != NIL && (*iter).getDateInSeconds() <= (today - 60*60*24*7)) {
			if((*iter).getStatus() == DONE) {
				(*iter).changeStatus(ARCHIVE);
				edited = true;
			}
		}
	}
	
	return edited;
}

void AutoEditStatus::editInTxtStorage(vector<Task> store) {
	
	assert(store.size() != NIL);

	vector<string> forTxtRewrite;
	string data;
	Parser parse;
	Storage storage;
	
	for(vector<Task>::iterator iter = store.begin(); iter != store.end(); iter++) {
		data = parse.convertToString(*iter);
		forTxtRewrite.push_back(data);
	}
	
	storage.rewrite(forTxtRewrite);
	
	return;
}

void AutoEditStatus::execute(vector<Task> & store) {

	ofstream writeFile;
	writeFile.open(FILENAME_LOGGER.c_str(), ofstream::out | ofstream::app);
	writeFile << "Attempting to autoChangeStatus" << endl;
	writeFile.close();
	
	if(store.size() == NIL) {
		return;
	}

	bool edited = false;

	time_t rawtime;
	/* current time in seconds */
	time(&rawtime);  
 
	struct tm * currentTime;
 
	currentTime = localtime(&rawtime);
 
	// making current time point to today at 00:00
	(*currentTime).tm_hour = 0;                             
	(*currentTime).tm_min = 0;
	(*currentTime).tm_sec = 0;
 
	time_t today = mktime(currentTime);

	bool archiveEdit = archive(store,today);
	bool overdueEdit = overdue(store,today);

	if(archiveEdit || overdueEdit) {
		editInTxtStorage(store);
	}

	return;
}

bool AutoEditStatus::overdue(vector<Task> & store, const time_t & today) {
	
	assert(store.size() != NIL);
	assert(today != NIL);

	ofstream writeFile;
	writeFile.open(FILENAME_LOGGER.c_str(), ofstream::out | ofstream::app);
	writeFile << "Checking for overdue items" << endl;
	writeFile.close();
	
	bool edited = false;

	// if WIP && past today, mark as CMI
	for(vector<Task>::iterator iter = store.begin(); iter != store.end(); iter++) {
		if((*iter).getDateInSeconds() != NIL && (*iter).getDateInSeconds() < today) {
			if((*iter).getStatus() == WIP) {
				(*iter).changeStatus(CMI);
				edited = true;
			}
		}
	
	}
	
	return edited;
}